﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;


namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCDanhSachGV : UserControl
    {
        GiaoVienBUO gvBUO = new GiaoVienBUO();

        int id;
        public String id_GV, id_Khoi, id_MH;

        public UCDanhSachGV()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
        }

        private void txtTim_Click(object sender, EventArgs e)
        {
            if (rdTimTheoTen.Checked)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = gvBUO.TimTheoTenGiaoVien(txtTimTen.Text);
            }
            else
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = gvBUO.TimTheoMaGiaoVien(txtTimMa.Text);
            }
        }

        private void rdTimTheoTen_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTen.Enabled = true;
            txtTimMa.Enabled = false;
        }

        private void rdTimTheoMa_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTen.Enabled = false;
            txtTimMa.Enabled = true;
        }

        private void txtTimTen_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = gvBUO.TimTheoTenGiaoVien(txtTimTen.Text);
        }

        private void txtTimMa_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = gvBUO.TimTheoMaGiaoVien(txtTimMa.Text);
        }

        private void txtTimTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rdTimTheoTen.Checked == true)
                {
                    if (txtTimTen.Text.Equals(""))
                    {
                        MessageBox.Show("Chưa nhập tên giáo viên cần tìm");
                    }
                    else
                    {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = gvBUO.TimTheoTenGiaoVien(txtTimTen.Text);
                            dataGridView1.Visible = true;
                    }
                }
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_GV = dataGridView1.Rows[id].Cells[0].Value.ToString();
                        id_Khoi = dataGridView1.Rows[id].Cells[1].Value.ToString();
                        id_MH = dataGridView1.Rows[id].Cells[2].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void thêmMóiGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemGiaoVien a = new UCThemGiaoVien();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void sửaGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaGiaoVien a = new UCSuaGiaoVien(id_GV, id_Khoi, id_MH);

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void xóaGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu giáo viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (gvBUO.Delete(id_GV))
                {
                    MessageBox.Show("Xóa giáo viên thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa giáo viên thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            GiaoVienReport r = new GiaoVienReport();
            r.DataSource = dataGridView1.DataSource;
            r.Load();
            r.ShowPreviewDialog();
        }
    }
}
